package cn.edu.zut.cs.service.impl;

import cn.edu.zut.cs.pojo.DataVO;
import cn.edu.zut.cs.pojo.User;
import cn.edu.zut.cs.pojo.Weighing;
import cn.edu.zut.cs.mapper.WeighingMapper;
import cn.edu.zut.cs.pojo.WeighingVO;
import cn.edu.zut.cs.service.WeighingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.Resource;
import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 称重数据管理 服务实现类
 * </p>
 *
 * @author weighing
 * @since 2021-06-16
 */
@Service
public class WeighingServiceImpl extends ServiceImpl<WeighingMapper, Weighing> implements WeighingService {

    @Resource
    private WeighingMapper weighingMapper;
    @Resource
    private WeighingService weighingService;
    @Override
    public List<Weighing> selectAllWeighting(User user) {
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.eq("weicompany",user.getUsercompany());
        }else{
            queryWrapper.eq("weicompany",user.getUsercompany()).eq("weilocation",user.getUserlocation());
        }
        List<Weighing> weighings = weighingMapper.selectList(queryWrapper);
        return weighings;
    }

    @Override
    public List<Weighing> selectWeightingByTime(User user,Date date) {
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.ge("weighttime",date).eq("weicompany",user.getUsercompany());
        }else{
            queryWrapper.ge("weighttime",date).eq("weicompany",user.getUsercompany()).eq("weilocation",user.getUserlocation());
        }
        List<Weighing> weighings = weighingMapper.selectList(queryWrapper);
        return weighings;
    }

    @Override
    public WeighingVO barVOListByYear(User user,Integer year) {
        WeighingVO weighingVO=new WeighingVO();
        List<Integer> names=new ArrayList<>();
        List<Double> values=new ArrayList<>();
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.eq("weicompany",user.getUsercompany());
        }else{
            queryWrapper.eq("weicompany",user.getUsercompany()).eq("weilocation",user.getUserlocation());
        }
    //    queryWrapper.eq()
        List<Weighing> weighings = weighingMapper.selectList(queryWrapper);
        for(int i=1;i<=12;i++){
            names.add(i-1,i);
            values.add(i-1,0.00);
        }
        for (Weighing weighing:weighings) {
            Calendar c=Calendar.getInstance();
            c.setTime(weighing.getWeighttime());
            if(c.get(Calendar.YEAR)==year){
                values.set(c.get(Calendar.MONTH), values.get(c.get(Calendar.MONTH))+weighing.getWeight());
            }
        };
        weighingVO.setNames(names);
        weighingVO.setValues(values);
        return weighingVO;
    }

    @Override
    public WeighingVO barVOListByMonth(User user,Integer year,Integer month) {
        WeighingVO weighingVO=new WeighingVO();
        List<Integer> names=new ArrayList<>();
        List<Double> values=new ArrayList<>();
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.eq("weicompany",user.getUsercompany());
        }else{
            queryWrapper.eq("weicompany",user.getUsercompany()).eq("weilocation",user.getUserlocation());
        }
        List<Weighing> weighings = weighingMapper.selectList(queryWrapper);
        for(int i=1;i<=32;i++){
            names.add(i-1,i);
            values.add(i-1,0.00);
        }
        for (Weighing weighing:weighings) {
            Calendar c=Calendar.getInstance();
            c.setTime(weighing.getWeighttime());
            if(c.get(Calendar.MONTH)+1==month&&c.get(Calendar.YEAR)==year){
                values.set(c.get(Calendar.DAY_OF_MONTH), values.get(c.get(Calendar.DAY_OF_MONTH))+weighing.getWeight());
            }
        };
        weighingVO.setNames(names);
        weighingVO.setValues(values);
        return weighingVO;
    }

    @Override
    public List<DataVO> barVOListByDay(User user, Integer year, Integer month, Integer day) {
        WeighingVO weighingVO=new WeighingVO();
        List<DataVO> dataVOS=new ArrayList<>();
        dataVOS.add(new DataVO("1号仓库",0.0));
        dataVOS.add(new DataVO("2号仓库",0.0));
        dataVOS.add(new DataVO("3号仓库",0.0));
        dataVOS.add(new DataVO("4号仓库",0.0));
        dataVOS.add(new DataVO("5号仓库",0.0));
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.eq("weicompany",user.getUsercompany());
        }else{
            queryWrapper.eq("weicompany",user.getUsercompany()).eq("weilocation",user.getUserlocation());
        }
        List<Weighing> weighings = weighingMapper.selectList(queryWrapper);

        for (DataVO dataVO:dataVOS) {
            for(Weighing weighing:weighings){
                Calendar c=Calendar.getInstance();
                c.setTime(weighing.getWeighttime());
                if(user.getUsercompany().equals(weighing.getWeicompany())&&weighing.getWeilocation().equals(dataVO.getNames())&&c.get(Calendar.MONTH)+1==month&&c.get(Calendar.YEAR)==year&&c.get(Calendar.DATE)==day){
//                    dataVO.set(c.get(Calendar.DAY_OF_MONTH), values.get(c.get(Calendar.DAY_OF_MONTH))+weighing.getWeight());
                    dataVO.setValues(dataVO.getValues()+weighing.getWeight());
                }
            }

        };
        return dataVOS;
    }

    @Override
    public List<DataVO> barVOListByLastMonth(User user, Integer year, Integer month) {
        WeighingVO weighingVO=new WeighingVO();
        List<DataVO> dataVOS=new ArrayList<>();
        dataVOS.add(new DataVO("1号仓库",0.0));
        dataVOS.add(new DataVO("2号仓库",0.0));
        dataVOS.add(new DataVO("3号仓库",0.0));
        dataVOS.add(new DataVO("4号仓库",0.0));
        dataVOS.add(new DataVO("5号仓库",0.0));
        QueryWrapper<Weighing> queryWrapper = new QueryWrapper<>();
        if(user.getUserlevel()==1){
            queryWrapper.eq("weicompany",user.getUsercompany());
        }else{
            queryWrapper.eq("weicompany",user.getUsercompany()).eq("weilocation",user.getUserlocation());
        }
        List<Weighing> weighings = weighingMapper.selectList(queryWrapper);

        for (DataVO dataVO:dataVOS) {
            for(Weighing weighing:weighings){
                Calendar c=Calendar.getInstance();
                c.setTime(weighing.getWeighttime());
                if(user.getUsercompany().equals(weighing.getWeicompany())&&weighing.getWeilocation().equals(dataVO.getNames())&&c.get(Calendar.MONTH)+1==month-1&&c.get(Calendar.YEAR)==year){
//                    dataVO.set(c.get(Calendar.DAY_OF_MONTH), values.get(c.get(Calendar.DAY_OF_MONTH))+weighing.getWeight());
                    dataVO.setValues(dataVO.getValues()+weighing.getWeight());
                }
            }

        };
        return dataVOS;
    }

    @Override
    public Double countWeight(User user) {
        List<Weighing> weighings = weighingService.selectAllWeighting(user);
        Double count=0.00;
        for (Weighing weighing:weighings) {
            count=count+weighing.getWeight();
        }
        return count;
    }



}
